package com.itheima.controller.store.catalog;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Catalog;
import com.itheima.domain.Course;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/store/catalog/*")
public class CatalogServlet extends BaseServlet {


    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");

      List<Catalog> list=catalogService.findAll(page,pageSize);
        PageInfo<Catalog> pageInfo = new PageInfo<>(list);
        req.setAttribute("page",pageInfo);

        req.getRequestDispatcher("/pages/store/catalog/list.jsp").forward(req,resp);

    }
    private void selectCourse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");


    List<Course> courseList=catalogService.selectCourse();
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(courseList);
        resp.getWriter().write(s);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);

        catalogService.save(catalog);
        findAll(req,resp);
    }
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String id = req.getParameter("id");
        Catalog catalog = catalogService.findById(id);
        req.setAttribute("catalog",catalog);
        req.getRequestDispatcher("/pages/store/catalog/update.jsp").forward(req,resp);
    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        Catalog catalog = BeanUtil.fillBean(req, Catalog.class);
        catalogService.update(catalog);


        findAll(req,resp);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String id = req.getParameter("id");
        catalogService.delete(id);

        findAll(req,resp);



    }
}
